package com.example.javabasic.thread.classtwo.workthread.demo2.pool;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @description 任务线程池
 * @Author Lin FuYuan
 * @date 2021/2/25
 */
public class TaskThreadPool {
    public static ThreadPoolTaskExecutor getExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //核心线程池大小
        executor.setCorePoolSize(5);
        //最大线程数
        executor.setMaxPoolSize(5);
        //队列容量
        executor.setQueueCapacity(1000);
        //线程活跃数
        executor.setKeepAliveSeconds(2);
        //线程名字前缀
        executor.setThreadNamePrefix("task_thread_pool");
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        //关闭时等待线程执行完成
        executor.setWaitForTasksToCompleteOnShutdown(false);
        executor.initialize();
        return executor;
    }
}
